﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liger.Data;
using Liger.Model;

namespace Test
{
    public class NorthwindExcuteTest : BaseTest
    {
        public NorthwindExcuteTest(DbContext db)
            : base(db)
        {
        }

        /// <summary>
        /// 测试 Any
        /// </summary>
        public void TestAny()
        {
            var result = Db.From<Orders>().Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).Exist();

            this.AssertReturn(result == true);
        }


        /// <summary>
        /// 测试 Count
        /// </summary>
        public void TestCount()
        {
            var result = Db.From<Orders>().Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).Count();

            this.AssertReturn(result == 59);
        }

        /// <summary>
        /// 测试 First
        /// </summary>
        public void TestFirst()
        {
            var entity = Db.From<Orders>().Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToFirst();

            this.AssertReturn(entity.CustomerID == "VICTE");
        }

        /// <summary>
        /// 测试 List
        /// </summary>
        public void TestList()
        {
            var list = Db.From<Orders>().Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToList();

            this.AssertReturn(list.Count == 59);
        }


        /// <summary>
        /// 测试 ToScalar
        /// </summary>
        public void TestToScalar()
        {
            var Scalar = Db.From<Orders>().Select(Orders._.OrderID).Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToScalar();

            this.AssertReturn((int)Scalar == 10251);
        }

        /// <summary>
        /// 测试 ToScalar2
        /// </summary>
        public void TestToScalar2()
        {
            var Scalar = Db.From<Orders>().Select(Orders._.OrderID).Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToScalar<int>();

            this.AssertReturn(Scalar == 10251);
        }

        /// <summary>
        /// 测试 ToDataSet
        /// </summary>
        public void TestToDataSet()
        {
            var ds = Db.From<Orders>().Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToDataSet();

            this.AssertReturn(ds.Tables[0].Rows.Count == 59);
        }



        /// <summary>
        /// 测试 只检索指定几个字段
        /// </summary>
        public void TestListWithSelectFields()
        {
            var ListWithSelectFields = Db.From<Orders>()
                .Select(Orders._.OrderID,Orders._.OrderDate,Orders._.CustomerID)
                .Where(Orders._.Freight >= 40 && Orders._.Freight <= 50).ToList();

            this.AssertReturn(ListWithSelectFields.Count == 59);
        }
    }
}
